//
//  SearchViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/28.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchModel.h"
#import "SearchTableviewCell.h"
#import "SearchDetailViewController.h"
@interface SearchViewController ()<UISearchBarDelegate, UISearchResultsUpdating, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *seartableview;//搜索tableView
@property (nonatomic, retain) UISearchController *search;//搜索控制视图
@property (nonatomic, copy) NSString *strHeight;//高亮设置
@property (nonatomic, retain) NSMutableArray *dataArr;//tableview数据
@end

@implementation SearchViewController

-(void)dealloc
{
    [_seartableview release];
    [_search release];
    [_strHeight release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:@selector(set)];
#pragma mark - 搜索
    //初始化搜索视图
    self.search = [[UISearchController alloc]initWithSearchResultsController:nil];
    //签订协议
    _search.searchResultsUpdater = self;
    //不让下拉
    _search.dimsBackgroundDuringPresentation = NO;
    //隐藏导航栏
    _search.hidesNavigationBarDuringPresentation = NO;
    //占位符
    _search.searchBar.placeholder = @"搜索感兴趣的内容";
    //搜索栏风格
    _search.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    //添加
    self.navigationItem.titleView = _search.searchBar;
    //键盘
    [_search release];
#pragma mark - tableview
    //创建
    self.seartableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    //代理
    self.seartableview.delegate = self;
    self.seartableview.dataSource = self;
    //添加
    [self.view addSubview:self.seartableview];
    [_seartableview release];
    
    //重用池
    [_seartableview registerClass:[SearchTableviewCell class] forCellReuseIdentifier:@"cell"];
}
#pragma mark - tableview必须实现的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    SearchModel *s = _dataArr [indexPath.row];
//    //高亮设置  
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:s.title];
    //取范围
    NSRange range = [s.title rangeOfString:_strHeight];
    [attStr setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName: [UIFont systemFontOfSize:20]} range:range];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:s.picurl]];
    cell.title.attributedText = attStr;
    
    cell.price.text =[@"￥" stringByAppendingString:s.yh_price];
    cell.sum.text = [s.sum stringByAppendingString:@"人买"];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
#pragma mark - 搜索栏事先协议方法
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    self.strHeight = [self.search.searchBar text];//搜索内容转换字符串
    if ([_strHeight containsString:@" "]) {
        //如果有空格不执行
    }
    else
    {
        //中文转码
        NSString *url = [_strHeight stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:_strHeight]];
        NSString *str =[[@"http://m.shougongke.com/index.php?c=query&a=index&" stringByAppendingString:[NSString stringWithFormat:@"q=%@",url]] stringByAppendingString:@"&type=shop&vid=15"];
        [AFNnetWork GETwithURL:str completion:^(id result) {
            NSArray *arr = result[@"data"][@"hits"];
            self.dataArr = [SearchModel modelHandlerWithArray:arr];//数组
            NSLog(@"%@",arr);
            [self.seartableview reloadData];//刷新
        }];
    }
}
- (void)set
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark - tableview点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchDetailViewController *sea = [[SearchDetailViewController alloc]init];
    SearchModel *seamodel = _dataArr[indexPath.row];
    sea.uid = seamodel.num_iid;
    [self.navigationController pushViewController:sea animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
